/**
 * zoneland.net Inc.
 * Copyright (c) 2002-2013 All Rights Reserved.
 */
package net.zoneland.knowledge.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import com.google.common.collect.Lists;

/**
 * 树形节点类，于ZTree对应.
 * @author ypz
 * @version $Id: Tree.java, v 0.1 2013-5-9 下午4:07:05 ypz Exp $
 */
public class TreeNode<T> implements Serializable {

    private static final long serialVersionUID = 7248103816553131514L;
    private String id;
    private String name;
    private String pId;
    private boolean open;
    private T data;
    private List<TreeNode<T>> children = new ArrayList<TreeNode<T>>();

    private boolean checked;

    private boolean isParent;

    private String title;

    private String otherParam;

    private String mainPosition; //是否为主职

    private boolean clickable = true;
    /**
     * Getter method for property <tt>checked</tt>.
     *
     * @return property value of checked
     */
    public boolean isClickable() {
        return clickable;
    }
    /**
     * Setter method for property <tt>checked</tt>.
     *
     * @param clickable
     *            value to be assigned to property checked
     */
    public void setClickable(final boolean clickable) {
        this.clickable = clickable;
    }
    /**
     * Getter method for property <tt>checked</tt>.
     *
     * @return property value of checked
     */
    public String getId() {
        return id;
    }
    /**
     * Setter method for property <tt>checked</tt>.
     *
     * @param id
     *            value to be assigned to property checked
     */
    public void setId(final String id) {
        this.id = id == null ? null : id.trim();
    }
    /**
     * Getter method for property <tt>checked</tt>.
     *
     * @return property value of checked
     */
    public String getName() {
        return name;
    }
    /**
     * Setter method for property <tt>checked</tt>.
     *
     * @param name
     *            value to be assigned to property checked
     */
    public void setName(final String name) {
        this.name = name == null ? null : name.trim();
    }
    /**
     * Getter method for property <tt>checked</tt>.
     *
     * @return property value of checked
     */
    public String getpId() {
        return pId;
    }
    /**
     * Setter method for property <tt>checked</tt>.
     *
     * @param pId
     *            value to be assigned to property checked
     */
    public void setpId(final String pId) {
        this.pId = pId == null ? null : pId.trim();
    }
    /**
     * Getter method for property <tt>checked</tt>.
     *
     * @return property value of checked
     */
    public boolean isOpen() {
        return open;
    }
    /**
     * Setter method for property <tt>checked</tt>.
     *
     * @param open
     *            value to be assigned to property checked
     */
    public void setOpen(final boolean open) {
        this.open = open;
    }
    /**
     * Getter method for property <tt>checked</tt>.
     *
     * @return property value of checked
     */
    public T getData() {
        return data;
    }
    /**
     * Setter method for property <tt>checked</tt>.
     *
     * @param data
     *            value to be assigned to property checked
     */
    public void setData(final T data) {
        this.data = data;
    }
    /**
     * Getter method for property <tt>checked</tt>.
     *
     * @return property value of checked
     */
    public List<TreeNode<T>> getChildren() {
        return Optional.ofNullable(children).map(it -> Lists.newArrayList(it.iterator())).orElse(null);
    }
    /**
     * Setter method for property <tt>checked</tt>.
     *
     * @param children
     *            value to be assigned to property checked
     */
    public void setChildren(final List<TreeNode<T>> children) {
        this.children = Optional.ofNullable(children).map(it -> Lists.newArrayList(it.iterator())).orElse(null);
    }

    @Override
    public String toString() {
        return toString(0);
    }

    /**
     * 获取层级.
     * @param level 层级
     * @return 对象String字符串
     */
    private String toString(final int level) {
        int levelTmp = level;
        levelTmp++;
        String result = this.getName() + this.isParent;
        if (this.children.size() > 0) {
            for (TreeNode<T> child : this.children) {
                result = result + "\n" + tableTimes(levelTmp) + child.toString(levelTmp);
            }
        }
        return result;
    }

    /**
     * 表格.
     * @param level 层级
     * @return 表格字符串
     */
    private String tableTimes(final int level) {
        String table = "";
        for (int i = 0; i < level; i++) {
            table += "--";
        }
        return table;
    }

    /**
     * Getter method for property <tt>checked</tt>.
     *
     * @return property value of checked
     */
    public boolean isChecked() {
        return checked;
    }

    /**
     * Setter method for property <tt>checked</tt>.
     *
     * @param checked
     *            value to be assigned to property checked
     */
    public void setChecked(final boolean checked) {
        this.checked = checked;
    }

    /**
     * Getter method for property <tt>isParent</tt>.
     *
     * @return property value of isParent
     */
    public boolean isParent() {
        return isParent;
    }

    /**
     * Setter method for property <tt>isParent</tt>.
     *
     * @param isParent
     *            value to be assigned to property isParent
     */
    public void setParent(final boolean isParent) {
        this.isParent = isParent;
    }

    /**
     * Getter method for property <tt>title</tt>.
     *
     * @return property value of title
     */
    public String getTitle() {
        return title;
    }

    /**
     * Setter method for property <tt>title</tt>.
     *
     * @param title
     *            value to be assigned to property title
     */
    public void setTitle(final String title) {
        this.title = title;
    }

    /**
     * Getter method for property <tt>otherParam</tt>.
     *
     * @return property value of otherParam
     */
    public String getOtherParam() {
        return otherParam;
    }

    /**
     * Setter method for property <tt>otherParam</tt>.
     *
     * @param otherParam
     *            value to be assigned to property otherParam
     */
    public void setOtherParam(final String otherParam) {
        this.otherParam = otherParam;
    }
    /**
     * Getter method for property <tt>otherParam</tt>.
     *
     * @return property value of otherParam
     */
    public String getMainPosition() {
        return mainPosition;
    }

    /**
     * Setter method for property <tt>otherParam</tt>.
     *
     * @param mainPosition
     *            value to be assigned to property otherParam
     */
    public void setMainPosition(final String mainPosition) {
        this.mainPosition = mainPosition;
    }

}
